package me.zingon.doc.service.impl;

import me.zingon.doc.model.User;
import me.zingon.doc.service.DocumentService;
import me.zingon.doc.dao.DocumentDao;
import me.zingon.doc.model.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
* Created by ZTCGenerator<zingon@aliyun.com> on 2018-5-31.
*/
@Service
public class DocumentServiceImpl implements DocumentService {

    @Autowired
    DocumentDao documentDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insert(Document document){
        return documentDao.save(document);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int delete(Document document){
        return documentDao.del(document);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteByIds(Integer[] ids) {
        return  documentDao.deleteByIds(ids);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int update(Document document){
//        document.setContent(HtmlUtils.htmlEscape(document.getContent()));
        return documentDao.update(document);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Document getById(Integer id){
        return documentDao.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<Document> list(Document document){
        return documentDao.list(document);
    }

//    ------------------


    @Override
    public Document getByUrlWithUser(String url, User user) {
        return documentDao.getByUrlWithUserId(url,user.getId());
    }


}